import {httpSever} from './index'
export const getGoods=(data:object)=>httpSever({
    method:'get',
    url:'getGoods',
})
import instance from './index'

//短token过期，携带长token重新请求token信息
export const refreshToken = () => {
  const r_tk = localStorage.getItem('refreshToken') //获取长token
  if (r_tk) {
    instance
      .get(
        'zhc/refresh',
        Object.assign(
          {},
          {
            headers: { refreshToken: r_tk } //设置请求头
          }
        )
      )
      .then((res: any) => {
        //长token失效，退出登录
        console.log(res.data.code, 'res.data.code')
        if (res.data.code === 401) {
          // flag = false
          localStorage.removeItem('refreshToken')
        } else if (res.data.code === 200) {
          console.log(res.data, '200')
          // 存储新的token
          localStorage.setItem('accessToken', res.data.accessToken)
          localStorage.setItem('refreshToken', res.data.refreshToken)
        }
      })
  } else {
    console.log('没有长token')
    // 返回登录页面
    clearUserState();
    redirectToLogin();
  }
}

// 假设的函数，用于重新发送请求
export const retryRequest = (config: any, newToken: any) => {
  config.headers['Authorization'] = `Bearer ${newToken}`;
  return instance(config);
};

// 假设的函数，用于清除用户状态
export const clearUserState = (): void => {
  localStorage.removeItem('accessToken');
  localStorage.removeItem('refreshToken');
  localStorage.removeItem('user');
  // 其他清除用户状态的操作
};

// 假设的函数，用于重定向到登录页面
export const redirectToLogin = (): void => {
  window.location.href = '/login';
};
